﻿using BJoin.Application.BObject;
using BJoin.Application.Entity;
using DapperExtensions;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BJoin.Application.IRepository
{
    public interface IBaseRepository<T, TKey, TExt> where T : Entity<TKey, TExt> where TExt:BObjExt
    {
        #region 异步
        /// <summary>
        /// 满足条件的记录数量
        /// </summary>
        /// <param name="conditions"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        Task<long> CountAsync(IDatabase database, object predicate =null);
        /// <summary>
        /// 通过主键获取实体对象
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        Task<T> GetAsync(IDatabase database, TKey id);
        /// <summary>
        /// 带参数的查询满足条件的数据
        /// </summary>
        /// <param name="conditions">条件</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        Task<IEnumerable<T>> GetListAsync(IDatabase database, object predicate =null, String[] sorts=null);
        /// <summary>
        /// 带参数并将结果映射到具有Paging的强类型List
        /// </summary>
        /// <param name="pageNumber">页码</param>
        /// <param name="rowsPerPage">每页显示数据</param>
        /// <param name="predicate">查询条件</param>
        /// <param name="orderby">排序</param>
        /// <returns></returns>
        Task<Page<T>> GetListPagingAsync(IDatabase database, int pageNumber, int recordsPerPage, object predicate=null, String[] sorts=null);
        /// <summary>
        /// 带参数Set获取
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <param name="firstResult">第几条</param>
        /// <param name="maxResults">最大条数</param>
        /// <returns></returns>
        Task<IEnumerable<T>> GetListSetAsync(IDatabase database, int firstResult, int maxResults, object predicate=null, String[] sorts=null);
        /// <summary>
        /// 根据sql返回动态对象
        /// </summary>
        /// <param name="sql">sql</param>
        /// <returns></returns>
        Task<IEnumerable<dynamic>> GetDynamicAsync(IDatabase database, string sql);
        /// <summary>
        /// 插入一条记录并返回主键值
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<TKey> InsertAsync(IDatabase database, T entity);
        /// <summary>
        /// 插入多条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<IEnumerable<TKey>> InsertManyAsync(IDatabase database, IEnumerable<T> entities);
        /// <summary>
        /// 更新一整条数据(支持null)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync(IDatabase database, T entity, object predicate);
        /// <summary>
        /// 更新部分数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="predicate"></param>
        /// <param name="commandTimeout"></param>
        /// <returns>true/false</returns>
        Task<bool> UpdateSetAsync(IDatabase database, T entity, object predicate);
        /// <summary>
        /// 根据实体主键删除一条数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>true/false</returns>
        Task<bool> DeleteAsync(IDatabase database, TKey id);
        /// <summary>
        /// 根据实体删除一条数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>true/false</returns>
        Task<bool> DeleteAsync(IDatabase database, object predicate = null);
        /// <summary>
        /// 根据ids删除多条数据
        /// </summary>
        /// <param name="ids">ids</param>
        /// <returns>返回影响的行数</returns>
        Task<bool> DeleteAsync(IDatabase database, string ids);
        #endregion
    }
}
